package com.shtel.paas.monitorcenter.config.management.warning.contects.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shtel.paas.monitorcenter.config.management.warning.contects.entity.MonitorCenterHostInfomationVo;
import com.shtel.paas.monitorcenter.config.management.warning.contects.entity.alarmResponseDaoEntity;
import com.shtel.paas.monitorcenter.config.management.warning.contects.entity.alarmResponseDto;
import com.shtel.paas.monitorcenter.config.management.warning.contects.entity.alarmResponseEntity;
import com.shtel.paas.monitorcenter.config.management.warning.contects.entity.contectTypeDto;
import com.shtel.paas.monitorcenter.config.management.warning.contects.mapper.warningContectsDao;
import com.shtel.paas.monitorcenter.config.management.warning.contects.service.IWarningContectsService;
import com.shtel.paas.monitorcenter.view.dispaly.host.monitor.vo.selecthostMonitorTreeDataVo;

@Service
public class warningContectsService implements IWarningContectsService {

	@Autowired
	warningContectsDao dao;

	public PageInfo<alarmResponseEntity> selectAll(String ip, Integer page,
			Integer pageSize) {

		List<alarmResponseDaoEntity> resEntity = new ArrayList<alarmResponseDaoEntity>();
		PageHelper.startPage(page, pageSize);
		resEntity = dao.selectAll(ip);
		PageInfo<alarmResponseEntity> pageContent = new PageInfo(resEntity);
		List<String> ipList = dao.selectIp();
		List<alarmResponseEntity> responseList = new ArrayList<alarmResponseEntity>();
		for (alarmResponseDaoEntity forEntity : resEntity) {
			alarmResponseEntity response = new alarmResponseEntity();
			List<String> newIpList = new ArrayList<String>();

			for (String listIp : ipList) {
				if (listIp.contains("/")) {
					String[] ss = new String[200];
					ss = listIp.split("/");
					for (int i = 0; i < ss.length; i++)
						newIpList.add(ss[i]);
				} else {
					newIpList.add(listIp);
				}
			}

			List<String> distinctList = new ArrayList<String>();
			for (String distinctIp : newIpList) {
				if (!distinctList.contains(distinctIp)) {
					distinctList.add(distinctIp);
				}
			}

			for (String newIp : distinctList) {
				if (newIp.equals(forEntity.getIp())) {
					Collections.swap(distinctList, 0,
							distinctList.indexOf(forEntity.getIp()));
					break;
				}
			}
			response.setIp(distinctList);
			response.setDangercontact(forEntity.getDangercontact());

			response.setDangercontype(exchangeTypeIndex(forEntity
					.getDangercontype()));

			response.setId(forEntity.getHostid());
			response.setDangeremail(forEntity.getDangeremail());
			response.setDangerphone(forEntity.getDangerphone());
			response.setSeriouscontact(forEntity.getSeriouscontact());
			response.setSeriouscontype(exchangeTypeIndex(forEntity
					.getSeriouscontype()));
			response.setSeriousemail(forEntity.getSeriousemail());
			response.setSeriousphone(forEntity.getSeriousphone());
			response.setWarncontact(forEntity.getWarncontact());
			response.setWarncontype(exchangeTypeIndex(forEntity
					.getWarncontype()));
			response.setWarnemail(forEntity.getWarnemail());
			response.setWarnphone(forEntity.getWarnphone());
			responseList.add(response);
		}
		pageContent.setList(responseList);
		return pageContent;
	}

	public List<contectTypeDto> exchangeTypeIndex(int typeId) {
		List<contectTypeDto> typeList = new ArrayList<contectTypeDto>();
		contectTypeDto dto0 = new contectTypeDto();
		dto0.setTypeId(0);
		dto0.setTypeName("邮件短信");
		typeList.add(dto0);
		contectTypeDto dto1 = new contectTypeDto();
		dto1.setTypeId(1);
		dto1.setTypeName("邮件");
		typeList.add(dto1);
		contectTypeDto dto2 = new contectTypeDto();
		dto2.setTypeId(2);
		dto2.setTypeName("短信");
		typeList.add(dto2);
		for (contectTypeDto dto : typeList) {
			if (dto.getTypeId() == typeId) {
				Collections.swap(typeList, 0, typeList.indexOf(dto));
				break;
			}
		}
		return typeList;
	}

	/**
	 * 【保存配置】功能
	 */
	@Override
	public int updateWarningConytects(alarmResponseDto dto) {
		int result = 0;
		String[] ip = dto.getIp().split(",");
		dto.setHostid(Integer.parseInt(dto.getId()));
		result = dao.updateAlarmResponse(dto);
		for (String item : ip) {
			if (!"".equals(item)) {
				dto.setIp(item);
				Integer count = dao.selecthostIdIsExist(dto);// 查询表【alarm_contects】是否存在配置IP
				if (count > 0) {
					dao.updateDeleteContects(dto.getIp(),
							String.valueOf(dto.getHostid()));// DELETE表【alarm_contects】
				} else {
					insertWarningContects1(dto);// INSERT表【alarm_contects】
				}
			}
		}
		return result;
	}

	/**
	 * 【删除配置】工能
	 */
	@Override
	public int deleteWarningContects(String id) {
		if (dao.deleteAlarmResponse(id) > 0 && dao.deleteAlarmContects(id) > 0) {
			return 0;
		}
		return -1;

	}

	@Override
	public int insertWarningContects(alarmResponseDto dto) {
		dao.insertAlarmResponse(dto);
		String hostId = String.valueOf(dto.getHostid());
		System.out.println(hostId);
		String ip = dto.getIp();
		if (dao.insertAlarmContects(hostId, ip) > 0) {
			return 0;
		}
		return -1;
	}

	/**
	 * 【保存配置】功能如果IP没有被配置过的情况下，循环INSERT操作。
	 */
	@Override
	public int insertWarningContects1(alarmResponseDto dto) {
		String hostId = String.valueOf(dto.getId());
		String ip = dto.getIp();
		if (dao.insertAlarmContects(hostId, ip) > 0) {
			return 0;
		}
		return -1;
	}

	@Override
	public List<String> selectIpList() {
		List<String> ipList = dao.selectIp();
		List<String> newIpList = new ArrayList<String>();
		List<String> distinctList = new ArrayList<String>();
		for (String listIp : ipList) {
			if (listIp.contains("/")) {
				String[] ss = new String[200];
				ss = listIp.split("/");
				for (int i = 0; i < ss.length; i++)
					newIpList.add(ss[i]);
			} else {
				newIpList.add(listIp);
			}
		}
		for (String ip : newIpList) {
			if (!distinctList.contains(ip)) {
				distinctList.add(ip);
			}
		}
		return distinctList;
	}

	/**
	 * 查询未配置IP地址和systemName实现方法方法
	 * 
	 * @param:
	 * @author: zhangyy
	 * @Date: 2018.05.15
	 * @return: list
	 */
	@Override
	public List<MonitorCenterHostInfomationVo> selectIpsAndSystemName() {
		List<MonitorCenterHostInfomationVo> ipAndSystemNameList = dao
				.selectIpsAndSystemName();
		return ipAndSystemNameList;
	}

	/**
	 * 列表IP地址查询
	 * 
	 * @param:
	 * @author: zhangyy
	 * @Date: 2018.05.15
	 * @return: list
	 */
	@Override
	public List<Object> getIpTreeData() {
		// 从数据库取出的原始数据
		List<selecthostMonitorTreeDataVo> resultArray = new ArrayList<>();
		// 第一层数据
		List<selecthostMonitorTreeDataVo> parentArrayList = new ArrayList<>();
		// 第二层数据
		List<selecthostMonitorTreeDataVo> childArray = new ArrayList<>();
		// 返回结果
		List<Object> result = new ArrayList<>();
		resultArray = dao.selecIpTreeData();

		// 第一层节点数据和第二层节点数据分类
		for (selecthostMonitorTreeDataVo parent : resultArray) {
			if (parent.getCommentId() != null) {
				parent.setIsParent("true");
				if (parent.getCommentId().equals(parent.getParentId())) {// 第一层数据
					parent.setIsParent("true");
					parentArrayList.add(parent);
				} else {// 第二层数据
					childArray.add(parent);
				}
			}
		}

		// tree数据改造
		for (selecthostMonitorTreeDataVo parent : parentArrayList) {
			List<selecthostMonitorTreeDataVo> childList = new ArrayList<>();
			Map<String, Object> results = new HashMap<String, Object>();
			for (selecthostMonitorTreeDataVo child : childArray) {
				if (parent.getParentId().equals(child.getParentId())) {
					childList.add(child);
				}
			}
			results.put("name", parent.getName());
			results.put("children", childList);
			results.put("isParent", true);
			result.add(results);
		}
		return result;
	}

	/**
	 * 异步获取动态树的第三层节点数据
	 */
	@Override
	public List<selecthostMonitorTreeDataVo> asyncGetIpTreeData(
			String commentId, String hostId) {

		List<selecthostMonitorTreeDataVo> resultList = dao
				.asyncSelectIpTreeData(commentId);
		List<MonitorCenterHostInfomationVo> selectAllIp = dao
				.selectAllIp(hostId);
		for (selecthostMonitorTreeDataVo item : resultList) {
			for (MonitorCenterHostInfomationVo ip : selectAllIp) {
				if (ip != null) {
					if (ip.getIp().equals(item.getName())) {
						// item.setChkDisabled("true");
						item.setChecked("true");
						item.setHostId(ip.getSystemName());
					}
				}
			}
		}
		return resultList;
	}
}
